package com.learning.database;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper {

	static final String dbName = "demoDB";
	static final String tblUser = "tbl_Users";
	static final String username = "Username";
	static final String password = "Password";
	static final String email = "Email";

	public DatabaseHelper(Context context) {
		super(context, dbName, null, 33);
	}
	
	private static DatabaseHelper myDatabase;
	
	public static DatabaseHelper getInstance(Context context){
		if (myDatabase == null)
			myDatabase =  new DatabaseHelper(context);
		return myDatabase;
	}

	
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE " + tblUser + " (" + "ID"
				+ " INTEGER PRIMARY KEY   AUTOINCREMENT, " + username
				+ " TEXT NOT NULL, " + password + " TEXT NOT NULL, " + email
				+ " TEXT NOT NULL" + " )");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onCreate(db);
	}

	public boolean insertAccount(String us, String pass, String em) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(username, us);
		cv.put(password, pass);
		cv.put(email, em);
		long returnvalue = db.insert(tblUser, null, cv);
		db.close();
		return returnvalue != -1;
	}

	public boolean checkExistAccount(String user) {
		SQLiteDatabase db = this.getReadableDatabase();
		String[] columns = new String[] { "ID" };
		Cursor c = db.query(tblUser, columns, username + "=?",
				new String[] { user }, null, null, null);
		//db.close();
		return c.getCount() != 0;
	}

	public boolean checkLogin(String user, String pass) {
		SQLiteDatabase db = this.getReadableDatabase();
		String[] columns = new String[] { "ID" };
		Cursor c = db.query(tblUser, columns, username + "=?" + "and "
				+ password + "=?", new String[] { user, pass }, null, null,
				null);
		//db.close();
		return c.getCount() != 0;
	}
	
	public boolean updateAccount(String user, String pass) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(username, user);
		cv.put(password, pass);
		cv.put(email, "bao.hau@thegenius.com");
		int returnvalue = db.update(tblUser, cv, username + "=?", new String[]{user});
		//db.close();
		return returnvalue == 1;
	}
	
	public List<String> getListAccount(){
		List<String> lst = new ArrayList<String>();
		SQLiteDatabase db = this.getReadableDatabase();
		String[] columns = new String[] { username };
		Cursor c = db.query(tblUser, columns, null, null, null, null, null);
		if (c.moveToFirst()){
			   do{
			      String data = c.getString(c.getColumnIndex(username));
			      lst.add(data);
			   }while(c.moveToNext());
			}
		c.close();
		return lst;
	}
}
